﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Web.Enhancements.Rest.Templates
{
    /// <summary>
    /// Basic class for HTML Template with generic type for its data source. 
    /// All HTML Templates must be inherited from either this class or its non-generic version - HtmlTemplate.
    /// </summary>
    /// <typeparam name="TDataSource">Generic object type of DataSource for the HTML Template.</typeparam>
    public class HtmlTemplate<TDataSource> : UserControl, IHtmlTemplate<TDataSource>
    {
        /// <summary>
        /// Gets or sets data source for the HTML Template.
        /// </summary>
        public TDataSource DataSource { get; set; }

        /// <summary>
        /// Gets collection of temporary items used to pass additional data to HTML template.
        /// </summary>
        public Dictionary<string, object> TempData { get; set; }

        /// <summary>
        /// Deafult constructor.
        /// </summary>
        public HtmlTemplate()
            : base()
        {
            this.TempData = new Dictionary<string, object>();
        }

        /// <summary>
        /// Method renders HTML template with given data source and writes result into an HtmlTextWriter.
        /// </summary>
        /// <param name="dataSource">Data source for the template.</param>
        /// <param name="writer">HtmlTextWriter object to write rendered result to.</param>
        public void RenderTemplate(TDataSource dataSource, HtmlTextWriter writer)
        {
            this.DataSource = dataSource;
            this.RenderTemplate(writer);
        }

        /// <summary>
        /// Method renders HTML template and writes result into an HtmlTextWriter.
        /// </summary>
        /// <param name="writer">HtmlTextWriter object to write rendered result to.</param>
        public void RenderTemplate(HtmlTextWriter writer)
        {
            base.RenderControl(writer);
        }

        /// <summary>
        /// Gets the Session object for the current HTTP request.
        /// </summary>
        public new System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return this.Context.Session;
            }
        }

        /// <summary>
        /// Gets the Request object for the current HTTP request.
        /// </summary>
        public new HttpRequest Request
        {
            get
            {
                return this.Context.Request;
            }
        }

        /// <summary>
        /// Gets the Response object for the current HTTP response.
        /// </summary>
        public new HttpResponse Response
        {
            get
            {
                return this.Context.Response;
            }
        }

        /// <summary>
        /// Gets the Application object for the current HTTP request.
        /// </summary>
        public new HttpApplicationState Application
        {
            get
            {
                return this.Context.Application;
            }
        }

        /// <summary>
        /// Gets boolean value indicated if this is postback request or request made for a first time to the resource. 
        /// This value for HTML template is always false and implemented to support a user control interface.
        /// </summary>
        public new bool IsPostBack
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the Cache object for the current application domain.
        /// </summary>
        public new System.Web.Caching.Cache Cache
        {
            get
            {
                return this.Context.Cache;
            }
        }

        /// <summary>
        /// Gets the Session object for the current HTTP response.
        /// </summary>
        public new TraceContext Trace
        {
            get
            {
                return this.Context.Trace;
            }
        }

        /// <summary>
        /// Gets the Server object for the current HTTP request.
        /// </summary>
        public new HttpServerUtility Server
        {
            get
            {
                return this.Context.Server;
            }
        }
    }

    /// <summary>
    /// Basic class for non-generic version of HTML Template. 
    /// All HTML Templates must be inherited from either this class or its generic version.
    /// </summary>
    public class HtmlTemplate : HtmlTemplate<object>
    {
        /// <summary>
        /// Deafult constructor.
        /// </summary>
        public HtmlTemplate()
            : base()
        {
        }
    }
}
